package com.app.twth.ui.news;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.app.twth.app.Constant;
import com.app.twth.app.MyApplication;
import com.app.twth.base.BaseAndroidViewModel;
import com.app.twth.bean.BaseResponseEntity;
import com.app.twth.bean.LoadingBean;
import com.app.twth.net.JsonCallBack;
import com.app.twth.ui.adjust.recheck.ReCheck;
import com.app.twth.ui.adjust.redamage.ReDamage;
import com.app.twth.ui.adjust.remeasure.ReMeasureQr;
import com.app.twth.ui.adjust.retally.ReTally;
import com.app.twth.ui.finalprocess.FinalProcess;
import com.app.twth.ui.main.TaskQty;
import com.app.twth.utils.FastJsonUtil;
import com.app.twth.utils.PreferencesUtil;
import com.app.twth.utils.Utils;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;


/**
 * ****************************************************************
 * 文件名称: LoginViewModel
 * 文件描述:
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class NewsViewModel extends BaseAndroidViewModel<NewsDataModel> {

    public MutableLiveData<List<NewsBean>> newsLiveData = new MutableLiveData<>();
    public MutableLiveData<ReCheck> reCheckLiveData = new MutableLiveData<>();
    public MutableLiveData<ReMeasureQr> reMeasureLiveData = new MutableLiveData<>();
    public MutableLiveData<ReTally> reTallyLiveData = new MutableLiveData<>();
    public MutableLiveData<ReDamage> reDamageLiveData = new MutableLiveData<>();
    public MutableLiveData<FinalProcess> lpnItemInfoLiveData = new MutableLiveData<>();

    public NewsViewModel(@NonNull Application application) {
        super(application);
    }

    public void taskPendingList() {
        errorResultCode.setValue("");
        mModel.taskPendingList(new JsonCallBack<BaseResponseEntity<List<NewsBean>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<NewsBean>>> response) {
                BaseResponseEntity<List<NewsBean>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    List<NewsBean> data = baseResponseEntity.data;
                    if (data!=null){
                        String string = PreferencesUtil.getString(MyApplication.getAppInstance(), Constant.TASKNUM);
                        TaskQty searchTaskQty = new TaskQty();
                        if (!Utils.isEmpty(string)){
                            searchTaskQty =  FastJsonUtil.getObject(string,TaskQty.class);
                        }
                        if (searchTaskQty==null){
                            searchTaskQty = new TaskQty();
                        }
                        searchTaskQty.setTaskCount(data.size());

                        PreferencesUtil.putString(MyApplication.getAppInstance(), Constant.TASKNUM, FastJsonUtil.createJsonString(searchTaskQty));
                        EventBus.getDefault().post(response.body().data);
                    }
                    newsLiveData.setValue(data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
                errorResultCode.setValue(infoResultMsg);
            }

            @Override
            public void onError(String error) {
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void tallyReCheckInfo(NewsBean newsBean) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.tallyReCheckInfo(newsBean.getId() + "", new JsonCallBack<BaseResponseEntity<ReCheck>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<ReCheck>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<ReCheck> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    ReCheck data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    if (Utils.isEmpty(data.getSku())) {
                        data.setSku(newsBean.getSku());
                    }
                    reCheckLiveData.setValue(data);

                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
                errorResultCode.setValue(infoResultMsg);
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void tallyReTally(NewsBean newsBean) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.tallyReTally(newsBean.getId() + "", new JsonCallBack<BaseResponseEntity<ReTally>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<ReTally>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<ReTally> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    ReTally data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    data.setOrderId(newsBean.getInboundOrderId());
                    data.setTaskId(newsBean.getId());
                    reTallyLiveData.setValue(data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
                errorResultCode.setValue(infoResultMsg);
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void damageProcessInfo(NewsBean newsBean) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.damageProcessInfo(newsBean.getId() + "", new JsonCallBack<BaseResponseEntity<ReDamage>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<ReDamage>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<ReDamage> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    ReDamage data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    data.setOrderId(newsBean.getInboundOrderId());
                    data.setTaskId(newsBean.getId());
                    data.setContainerNo(newsBean.getContainerNo());
                    data.setLpn(newsBean.getIbLpn());
                    data.setSku(newsBean.getSku());
                    data.setType(newsBean.getType());
                    reDamageLiveData.setValue(data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
                errorResultCode.setValue(infoResultMsg);
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void tallyReMeasure(NewsBean newsBean) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        ReMeasureQr reMeasureQr = new ReMeasureQr();
        reMeasureQr.setBarcode(newsBean.getSku());
        reMeasureQr.setOrderId(newsBean.getInboundOrderId());
        reMeasureQr.setTaskId(newsBean.getId());
        mModel.tallyReMeasure(reMeasureQr, new JsonCallBack<BaseResponseEntity<ReMeasureQr>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<ReMeasureQr>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<ReMeasureQr> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    ReMeasureQr data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }

                    reMeasureQr.setSkuUnit(data.getSkuUnit());
                    reMeasureLiveData.setValue(reMeasureQr);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
                errorResultCode.setValue(infoResultMsg);
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void returnFinalProcessItem(NewsBean newsBean) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.returnFinalProcessItem(newsBean.getBatchNo(), newsBean.getIbLpn(), new JsonCallBack<BaseResponseEntity<ArrayList<FinalProcess.ItemInfoDTO>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<ArrayList<FinalProcess.ItemInfoDTO>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<ArrayList<FinalProcess.ItemInfoDTO>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    FinalProcess finalProcess = new FinalProcess();
                    finalProcess.setIbLpnItemList(baseResponseEntity.data);
                    FinalProcess.ItemInfoDTO itemInfoDTO = new FinalProcess.ItemInfoDTO();
                    itemInfoDTO.setBatchNo(newsBean.getBatchNo());
                    itemInfoDTO.setIbLpn(newsBean.getIbLpn());
                    finalProcess.setItemInfo(itemInfoDTO);
                    lpnItemInfoLiveData.setValue(finalProcess);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }
}
